import type { RouteConfig } from "types/router";
import type { GetProp, MenuProps } from "antd";

type MenuItem = GetProp<MenuProps, "items">[number];
export default function useRouteToMenu() {
  const routeToMenu = (routes: RouteConfig[]): MenuItem[] => {
    return routes
      .filter((route) => !route.meta?.hideMenu)
      .map((item) => {
        const { meta, children } = item;
        if (!meta) return {} as MenuItem;
        const menuItem: Partial<MenuItem> = {
          key: meta.key,
          label: meta?.title,
          icon: meta?.icon,
          children: children?.length ? routeToMenu(children) : null,
          ...(children && { children: routeToMenu(children) }),
        };
        return menuItem as MenuItem;
      });
  };
  return routeToMenu;
}
